﻿using DotNetDAL.Entity;
using DotNetDAL.Entity.DbStruct;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace DotNetDAL.Connector
{
    /// <summary>
    /// 
    /// </summary>
    class SqlServerParamHelper
    {
        #region 生成实体参数

        /// <summary>
        /// 生成实体参数
        /// </summary>
        /// <param name="cahceData">数据库结构缓存数据</param>
        /// <param name="data">参数对象</param>
        /// <param name="paramPrefix">参数前缀。即在@后面增加字符串前缀</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static DbActionParam<System.Data.SqlClient.SqlParameter> CreateSqlEntityParameters(
            DbStructColumnCache cahceData, object data, string paramPrefix = null)
        {
            paramPrefix = string.IsNullOrEmpty(paramPrefix) ? "" : paramPrefix.Trim();

            DbActionParam<System.Data.SqlClient.SqlParameter> parObj = 
                new DbActionParam<System.Data.SqlClient.SqlParameter>();

            parObj.column = new List<string>();
            parObj.paramList = new List<System.Data.SqlClient.SqlParameter>();
            List<DbActionParamColumn> _parList = DbActionParamHelper.CreateParams(cahceData, data);
            foreach (DbActionParamColumn par in _parList)
            {
                parObj.column.Add(par.name);

                bool isDbNull = par.value == null || (par.value is string && string.IsNullOrEmpty(par.value as string));

                if (par.isnull_able == 0 && isDbNull)
                {
                    throw new Exception(string.Format("字段“{0}”不能为空", par.name));
                }

                parObj.paramList.Add(new System.Data.SqlClient.SqlParameter("@" + paramPrefix 
                    + par.name , isDbNull ? DBNull.Value : par.value));
            }
            return parObj;
        }

        #endregion

        /// <summary>
        /// 是否空值
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        private static bool IsDbNull(object Value)
        {
            return Value == null || (Value is string && string.IsNullOrEmpty(Value as string));
        }


        #region 生成数据库参数

        /// <summary>
        /// 生成数据库参数
        /// </summary>
        /// <param name="data">参数对象</param>
        /// <param name="paramPrefix">参数前缀</param>
        /// <returns></returns>
        public static List<System.Data.SqlClient.SqlParameter> CreateSqlParameters(object data, string paramPrefix = null )
        {
            List<System.Data.SqlClient.SqlParameter> parList = new List<System.Data.SqlClient.SqlParameter>();
            if (data != null)
            {
                paramPrefix = string.IsNullOrEmpty(paramPrefix) ? "" : paramPrefix.Trim();

                // 处理字典参数
                if (data is Dictionary<string, object>)
                {
                    Dictionary<string, object> _data = data as Dictionary<string, object>;
                    foreach (KeyValuePair<string, object> kvp in _data)
                    {
                        parList.Add(new System.Data.SqlClient.SqlParameter("@" + paramPrefix 
                            + kvp.Key, IsDbNull(kvp.Value) ? DBNull.Value : kvp.Value));
                    }

                }
                // 处理对象参数
                else
                {
                    Type type = data.GetType();
                    List<PropertyInfo> plist = new List<PropertyInfo>(type.GetProperties());
                    List<FieldInfo> flist = new List<FieldInfo>(type.GetFields());

                    if (plist != null)
                    {
                        foreach (PropertyInfo pInfo in plist)
                        {
                            object _value = pInfo.GetValue(data);
                            parList.Add(new System.Data.SqlClient.SqlParameter("@" + paramPrefix 
                                + pInfo.Name, IsDbNull(_value) ? DBNull.Value : _value));
                        }
                    }

                    if (flist != null)
                    {
                        foreach (FieldInfo fInfo in flist)
                        {
                            object _value = fInfo.GetValue(data);
                            parList.Add(new System.Data.SqlClient.SqlParameter("@" + paramPrefix 
                                + fInfo.Name, IsDbNull(_value) ? DBNull.Value : _value));
                        }
                    }

                }

            }

            return parList;
        }

        #endregion



    }
}
